﻿Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

Partial Class Plantilla1
    Inherits System.Web.UI.MasterPage

    'instanciar la conexion
    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)
    Dim Equipo As New Equipo

    'instanciar clases
    Dim cliente As New ClienteDAO

    Public Sub MostrarClienteSession()
        'lblSessionCodCli.Text = Session("cliente")
        If Not Session("cliente") = "" Then
            lblSessionCodCli.Text = Session("cliente")
            HlRegistrate.Visible = False
            LbLogueo.Visible = False
            lblTienes.Visible = False
        Else
            btnLogOut.Visible = False

        End If
    End Sub

    Protected Sub btnClienteReg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClienteReg.Click
        'instanciar el objeto  dao de cliente
        Dim cli As New ClienteBE
        cli.cod_cliente = txtCodCliReg.Text
        cli.password_cliente = txtPasswordReg.Text
        cli.email_cliente = txtEmailReg.Text
        '*
        cli.nom_cliente = txtNombreReg.Text
        cli.ape_cliente = txtApellidoReg.Text

        'ejecutar
        cliente.Agregar(cli)

        txtCodCliReg.Text = Nothing
        txtPasswordReg.Text = Nothing
        txtEmailReg.Text = Nothing
        '*
        txtNombreReg.Text = Nothing
        txtApellidoReg.Text = Nothing
    End Sub

    Protected Sub signin_submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles signin_submit.Click
        'Buscar Cliente
        Dim da As New SqlDataAdapter("select * from tb_cliente where cod_cliente=@cod and password_cliente=@pwd", cn)
        da.SelectCommand.Parameters.Add(
            "@cod", SqlDbType.VarChar).Value = txtcodcliLog.Text
        da.SelectCommand.Parameters.Add(
            "@pwd", SqlDbType.VarChar).Value = txtpwdLog.Text

        Dim tb As New DataTable
        da.Fill(tb)

        If tb.Rows.Count = 1 Then
            Session("cliente") = txtcodcliLog.Text
            Session("carrito").Clear()
            Response.Redirect("Home.aspx")
        Else
            MsgBox("ERROR! Ingrese de correctamente su Login y Contraseña")
        End If
    End Sub

    Protected Sub btnLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogOut.Click
        Session("cliente") = ""
        Session("carrito").Clear()
        lblSessionCodCli.Text = Session("cliente")
        Response.Redirect("Home.aspx")
    End Sub


    Sub cargarOpciones()

        Dim dt As DataTable = Equipo.ListadoParametro(" select  cod_marca cod, Name_marca nombre  from dbo.tb_marca WHERE estado = 'A' ")
        GridView1.DataSource = dt
        GridView1.DataBind()

        Dim strNombre As String = GridView1.Rows(0).Cells(1).Text.Trim


    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cargarOpciones()
        cargarCambio()

        If Session.Item("TotalItemCarrito") Is Nothing Then
            lblTotalItemsCargadosCarrito.Text = "0"

        Else

            lblTotalItemsCargadosCarrito.Text = CType(Session.Item("TotalItemCarrito"), String)

        End If

        If Session.Item("TotalSolesCarrito") Is Nothing Then
            lblTotalCarrito.Text = "0.00"

        Else

            lblTotalCarrito.Text = Format(CDbl(Session.Item("TotalSolesCarrito")), "##,##0.00")
            lblTotalCarritoDolares.Text = Format((CDbl(Session.Item("TotalSolesCarrito")) / CDbl(Session.Item("Cambio"))), "##,##0.00")

        End If
        ' Session.Add("cboMoneda", "01")

    End Sub

    Sub cargarCambio()
        Dim dt As DataTable = Equipo.ListadoParametro(" select  max(fecha) fecha , Tipcambio from TipoCambio group by  Tipcambio ")
        Dim strCambio As String = dt.Rows(0).Item("Tipcambio").ToString

        lblCambio.Text = "Dólares: S/. " & strCambio

        Session.Add("Cambio", strCambio)
       
    End Sub


    Protected Sub cboMoneda_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMoneda.SelectedIndexChanged
        If cboMoneda.SelectedValue.Trim = "01" Then
            Session.Add("cboMoneda", "01")
        Else
            Session.Add("cboMoneda", "02")
        End If
    End Sub

    Protected Sub BtnCarrito_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnCarrito.Click
        Response.Redirect("~/CarritoResumen.aspx")
    End Sub


End Class

